package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo06 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("账号");
        String  username = sc.nextLine();
        System.out.println("密码");
        String  password = sc.nextLine();
        System.out.println("昵称");
        String nick = sc.nextLine();

        try(   Connection conn =DBUtils.getConn()){
            String  sql = "select  id from user wherer username=?";
            PreparedStatement  ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs =ps.executeQuery();
            if (rs.next()){
                System.out.println("用户已经注册");
                return;//结束方法
            }
            //代码继续执行说明没有注册过
            String  regsql ="insert into user values(null,?,?,?)";
            PreparedStatement regPs =conn.prepareStatement(regsql);
            regPs.setString(1,username);
            regPs.setString(2,password);
            regPs.setString(3,nick);
            regPs.executeUpdate();//执行注册
            System.out.println("注册完成");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
